package com.atguigu.gmall.hive;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.json.JSONObject;

public class LogUDF extends UDF {

    public String evaluate(String line, String key) {

        if (StringUtils.isBlank(line)) {
            return "";
        }

        String[] split = line.split("\\|");

        if (split.length != 2) {
            return "";
        }

        String serverTime = split[0];
        String jsonStr = split[1];

        JSONObject jsonObject = new JSONObject(jsonStr);

        if("st".equals(key)){
            return serverTime;
        }else if("et".equals(key)){
            if(jsonObject.has("et")){
                return jsonObject.getString("et");
            }
        }else {
            JSONObject cm = jsonObject.getJSONObject("cm");
            if(cm.has(key)){
                return cm.getString(key);
            }
        }

        return "";
    }
}
